package com.tjetc.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tjetc.common.JsonResult;
import com.tjetc.dao.MerchantOrderMapper;
import com.tjetc.entity.OrderRequest;
import com.tjetc.service.MerchantOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;

//商家管理订单的业务类
@Service
public class MerchantOrderServiceImpl implements MerchantOrderService {

    @Autowired
    MerchantOrderMapper merchantOrderMapper;

    //获得所有的未处理的订单
    @Override
    public JsonResult getNewOrders(Integer merchantId) {
        return new JsonResult(0,"查询新订单成功",merchantOrderMapper.selectNewOrdersByMerchantId(merchantId));
    }

    //获得所有订单
    @Override
    public JsonResult getAllOrders(Integer merchantId) {
        return new JsonResult(0,"查询所有订单成功",merchantOrderMapper.selectAllOrdersByMerchantId(merchantId));
    }

    //根据订单id查询所有的菜
    @Override
    public JsonResult getDishByOrderId(Integer merchantId) {
        return new JsonResult(0,"查询订单详情成功",merchantOrderMapper.selectDishByOrderId(merchantId));
    }

    //完成订单，修改订单状态
    @Override
    public JsonResult finishOrder(Integer id) {
        return new JsonResult(0,"更新订单信息成功",merchantOrderMapper.updateStatusByOrderId(id));
    }

    @Override
    public JsonResult getAnOrder(Integer id) {
        return new JsonResult(0,"获得新订单成功",merchantOrderMapper.selectOrderById(id));
    }

    @Override
    public JsonResult getOrdersWith(OrderRequest orderRequest) {
        HashMap map=new HashMap<>();
        PageHelper.startPage(orderRequest.getPageNum(),orderRequest.getPageSize());
        map.put("merchantId",orderRequest.getMerchantId());
        if(orderRequest.getStatus()!=null)map.put("status",orderRequest.getStatus());
        if(orderRequest.getBeginDate()!=null)map.put("beginTime", LocalDateTime.of(orderRequest.getBeginDate(), LocalTime.MIN));
        if(orderRequest.getEndDate()!=null)map.put("endTime",LocalDateTime.of(orderRequest.getEndDate(),LocalTime.MAX));
        if(orderRequest.getBeginAmount()!=null)map.put("beginAmount",orderRequest.getBeginAmount());
        if(orderRequest.getEndAmount()!=null)map.put("endAmount",orderRequest.getEndAmount());
        if(orderRequest.getName()!=null)map.put("name",orderRequest.getName());
        PageInfo pageInfo=new PageInfo<>(merchantOrderMapper.selectOrdersWith(map));
        return new JsonResult(0,"查询订单里列表成功",pageInfo);
    }

    @Override
    public Integer countOrdersWith(OrderRequest orderRequest) {
        HashMap map=new HashMap<>();
        map.put("merchantId",orderRequest.getMerchantId());
        if(orderRequest.getStatus()!=null)map.put("status",orderRequest.getStatus());
        if(orderRequest.getBeginDate()!=null)map.put("beginTime", LocalDateTime.of(orderRequest.getBeginDate(), LocalTime.MIN));
        if(orderRequest.getEndDate()!=null)map.put("endTime",LocalDateTime.of(orderRequest.getEndDate(),LocalTime.MAX));
        if(orderRequest.getBeginAmount()!=null)map.put("beginAmount",orderRequest.getBeginAmount());
        if(orderRequest.getEndAmount()!=null)map.put("endAmount",orderRequest.getEndAmount());
        if(orderRequest.getName()!=null)map.put("name",orderRequest.getName());
        Integer num= merchantOrderMapper.countOrdersWith(map);
        System.out.println(num);
        return num;
    }


}
